# datetime : 23/9/2024 下午9:47
# -*-coding : utf-8 -*-
# Author : 油条测试
from data.get_data import GetData
from base.runByMethod import RunByMethod
from main.compare_res import Copare_Res
from data_config.qw_message_push import WeChatWorkNotifier


class RunCases():
    def __init__(self):
        self.data = GetData()
        self.run = RunByMethod()
        self.copare = Copare_Res()

    # 程序入口
    def run_case(self):
        # 获取用例总数
        caseCounts = self.data.get_nrows()
        for i in range(1, caseCounts):  # 123
            # 获取接口名称
            urlname = self.data.get_interfaceName(i)
            # 获取url
            url = self.data.get_url(i)
            # 获取请求方式
            requestsMethod = self.data.get_requestsMethod(i)
            # 是否带头信息
            headers = self.data.get_isHearders(i)
            if headers:
                isheader = self.data.get_herders()
            else:
                isheader = None
            # 是否是Json数据
            isJson = self.data.isJsonData(i)
            if isJson:
                requestsData = self.data.get_JsonData(i)
            else:
                # 请求数据
                requestsData = self.data.get_requestsData(i)
            # 预期结果
            exceptRes = self.data.get_exceptRes(i)
            # 实际结果
            actualRes = self.data.get_actualsRes(i)
            # 是否执行
            isExcute = self.data.isExcute(i)
            if isExcute:
                # print('请求数据：'+requestsMethod,isJson,url,type(requestsData),requestsData,headers,isheader)
                res = self.run.run_main(requestsMethod, isJson, url, requestsData, isheader)
                result = self.copare.compare_res(exceptRes, res)
                # 将测试结果写入exl
                self.data.write_value(i, result)
                # 打印执行的用例情况
                print("第{}个接口{}：已执行".format(i, urlname))
                print("请求方式:{}".format(requestsMethod))
                print("请求url:{}".format(url))
                print("请求数据:{}".format(requestsData))
                print("返回响应:{}".format(res))
                print("*" * 60 + "分割线" + "*" * 60)
            else:
                print("第{}个接口{}：未执行".format(i, urlname))
                print("*" * 60 + "分割线" + "*" * 60)
    #发送企微报告
    def qw_send(self):
        qw = WeChatWorkNotifier()
        qw.send_message()


if __name__ == '__main__':
    rb = RunCases()
    rb.run_case()
    rb.qw_send()
